package com.cloudbroker.bcs.platform.ltb.middle.util;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


/**
 * 微信相关接口
 *
 * @author xuyuzhu
 */
public class WechatUtil {
//	private static final String token = ConfigUtil.getString("wx.app.token");
//	private static final String appleId = ConfigUtil.getString("wx.app.id");
//	private static final String mchId = ConfigUtil.getString("wx.app.partnerId");
//	private static final String appSecret = ConfigUtil.getString("wx.app.secret");
//	public static final String notify_url = ConfigUtil.getString("wx.notify.url");

	public static final String OAUTH_BASEURL = "https://open.weixin.qq.com/connect/oauth2/authorize";
	private static final String GET_OAUTH_ACCRSS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";
	private static final String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";


	public static Log wcLogger = LogFactory.getLog("wcapi");

	/**
	 * 签名认证
	 */
	public static boolean checkSignature(String signature, String timestamp,
			String nonce) {

		wcLogger.debug("signature=" + StringUtils.defaultIfBlank(signature, "")
				+ ", timestamp=" + StringUtils.defaultIfBlank(timestamp, "")
				+ ", nonce=" + StringUtils.defaultIfBlank(nonce, ""));
		try {
			if (StringUtils.isBlank(signature)
					|| StringUtils.isBlank(timestamp)
					|| StringUtils.isBlank(nonce)) {
				return false;
			} else {
				String[] array = new String[] { ConfigUtil.getString("wx.app.token"), timestamp, nonce };
				Arrays.sort(array);
				return DigestUtils.shaHex(StringUtils.join(array)).equals(
						signature);
			}
		} catch (Exception e) {
			wcLogger.error("checkSignature error.");
		}
		return false;
	}



	/**
	 * 通过OAuth接口获取当前openId
	 */
	public static String getOAuthOpenIdFromRemote(String code,String company_id) {
		return getOAuthResult(code,company_id).openid;
	}

	public static OAuthResult getOAuthResult(String code,String company_id) {
		Map<String, Object> params = new HashMap();
		params.put("grant_type", "authorization_code");
		params.put("appid", ConfigUtil.getString(company_id,"wx.app.id",""));
		params.put("secret", ConfigUtil.getString(company_id,"wx.app.secret",""));
		params.put("code", code);
		String response = HttpUtil.doGet(GET_OAUTH_ACCRSS_TOKEN, params, "utf-8");
		return JSON.parseObject(response,OAuthResult.class);
	}

	public class OAuthResult {
		public String access_token;
		public int expires_in;
		public String refresh_token;
		public String openid;
		public String scope;

		public String getAccess_token() {
			return access_token;
		}

		public void setAccess_token(String access_token) {
			this.access_token = access_token;
		}

		public int getExpires_in() {
			return expires_in;
		}

		public void setExpires_in(int expires_in) {
			this.expires_in = expires_in;
		}

		public String getRefresh_token() {
			return refresh_token;
		}

		public void setRefresh_token(String refresh_token) {
			this.refresh_token = refresh_token;
		}

		public String getOpenid() {
			return openid;
		}

		public void setOpenid(String openid) {
			this.openid = openid;
		}

		public String getScope() {
			return scope;
		}

		public void setScope(String scope) {
			this.scope = scope;
		}
	}


}
